/**
 * 滚动数据加载
 * 修改记录
 * 	2012-12-27 将重新注册表格滑动交替行去除
 */
window.wd8.SCROLL = (function() {
	var scrollObj;// 用于放数据的对象，一般为div
	var tb;// 进行数据呈现的table
	var url;// ajax请求数据的url
	var param = {};// 请求的参数
	var iniSize=100;// 初始数据显示条数
	var incSize=50;// 滚动条滚动数据增量条数
	var ic=1;// 0代表页面不使用滚动加载，1代表页面使用滚动加载
	var currentStart=0;//当前屏记录起始位置
	var total;//总数据条数
	var formID;//formID
	// 进行表头和iniSize数据初始化
	var init = function() {
		currentStart=0;
		//jinjiayu20140523清空表格数据
		//$(tb).find("tbody").find("td").remove();
		//$(tb).find("tbody").remove();
		$(tb).empty();
		var jsonstr = {'pager.start':'0','pager.length':iniSize,'start':'0','length':iniSize};
		for(var item in jsonstr){
			if(param[item]!=undefined)continue;
			param[item]=jsonstr[item];
		}
		var table = tb;
		//数据显示表格定位属性
		var tableLeft;
		var tableTop;
		var tableWidth;
		if($(".tbl_listtwo").length>0){
			tableLeft = $(".tbl_listtwo").offset().left;
			tableTop = parseFloat($(".tbl_listtwo").offset().top,10);
			tableWidth = $(".tbl_listtwo").find('table').width();
		}
		var ScrollDataCountTop;
		//数据条数div显示数据初始化
		if($('.customscrollcount').length>0){
			$('.customscrollcount').empty();
		}
		//查询条件属性
		var search_contentheight ;
		if($("li .search_content").length>0){
			search_contentheight =  $("li .search_content").height();
		}
		// 第一次请求数据
		$.ajax({
			url:url,
			data:param,
			dataType:'text',
			type:'post',
			error:function(){},
			success:function(o){
				r = eval('('+o+')');
				total=r.total;
				var list = r.data;
				var tbody = document.createElement('tbody');
				var ScrollDataCountWidth = 150;
				//条数添加
				if($('.customscrollcount').length<=0&&$(".tbl_listtwo").length>0){//是否选择自定义添加
					if($('#ScrollDataCount').length<=0){//div是否已存在
						
						//配置高度
						if($("li .search_content").length<=0){//不存在高级查询
							ScrollDataCountTop = tableTop-15;
						}else{
							
							if(window.parent.selTermState==undefined){//无需设置查询条件显示、隐藏
								ScrollDataCountTop = tableTop-15;
							}else{
								if(!window.parent.selTermState){
									ScrollDataCountTop = tableTop-15-search_contentheight-15;
								}else{
									ScrollDataCountTop = tableTop-15;
								}
							}
							
						}
						//数据条数div显示初始化
						$("<div/>").attr("id","ScrollDataCount")
								.addClass('customscrollcount')
								.css("position","absolute")
								.css("left",tableLeft+tableWidth-ScrollDataCountWidth)
								.css("top",ScrollDataCountTop)
								.css("width",ScrollDataCountWidth)
								.css('text-align','right')
								.css('z-index','1')
								.appendTo($('body'));
					}
					//数据条数div显示数据添加
					$('#ScrollDataCount').html("Total "+total+"  ");
				}
				if($('.customscrollcount').length>0)$('.customscrollcount').html("Total "+total+"  ");
				//无数据
				if(total==0){
					var tr = document.createElement('tr');
					var td = document.createElement('td');
					$(td).css('width','100%');
					$(td).css('border','0');
					td.appendChild(document.createTextNode("No Data"));
					tr.appendChild(td);
					tbody.appendChild(tr);
					table.appendChild(tbody);
				}
				//jinjiayu20140523   隐藏“正在努力加载数据...”
				if($("#loadDiv").length>0){
					$("#loadDiv").hide();
				}
				if("callBack_"+formID in window){
					if(eval("callBack_"+formID) instanceof Function)
					eval("callBack_"+formID+"("+JSON.stringify(r)+",1)");
					if(submitStatus!=undefined)submitStatus = 4;
					//重新注册表格滑动交替行
					//addTableClass();
					return ;
				}
				// 显示数据
				wd8.each(list, function(os, i) {// tr
							var tr = document.createElement('tr');
							if(i%2!=0)$(tr).addClass("tdalt");
							wd8.each(os, function(o, j) {// td
										var td = document.createElement('td');
										td.innerHTML = o;
										tr.appendChild(td);
									});
							tbody.appendChild(tr);
						});
				table.appendChild(tbody);
				if(submitStatus!=undefined)submitStatus = 4;
				//重新注册表格滑动交替行
				//addTableClass();
			}
		});
	}
	//滚动请求
	var loading = 0;
	var scrollTopMark = 0;
	var scrollBottomMark = 0;
	var scrollBottomNow = 0;
	var scrollTopNow = 0;
	var table = tb;
	var start = 0;
	var isBack = false;
	var topDoc$ =  $(window.top.document);
	var customscrollcountoffsetTop = 0;
	//获取最外层DIV滚动条位置
	var topDocHeightControl = function(){
		var parentObj = window;
		var customscrollcount$;
		var recursion = function(parentObjParam){
			if(customscrollcount$.length>0){
				customscrollcountoffsetTop+=$('iframe',parentObjParam.parent.document).offset().top;
				if(parentObjParam!=window.top){
					recursion(parentObjParam.parent);
				}
			}
		};
		customscrollcount$ = $('.customscrollcount');
		customscrollcountoffsetTop = customscrollcount$.offset().top;
		recursion(parentObj);
		customscrollcountoffsetTop+=$('.iframeTop',window.top.document).height();
	}
	
	var locateTopDocScroll = function(){
		topDoc$.scrollTop(customscrollcountoffsetTop);
	}
	
	//滚动条滚动加载数据
	var scroll = function() {
		if(ic==0){
			return ;
		}
		$(scrollObj).scroll(function(){
			topDocHeightControl();
			if(scrollTopNow!=scrollObj.scrollTop)locateTopDocScroll();
			// 滚动条在标记后的1/2处
			scrollBottomNow = ((scrollObj.scrollHeight-scrollObj.scrollTop-$(scrollObj).height())/scrollObj.scrollHeight)*$(scrollObj).height();
			if(scrollBottomMark==0)scrollBottomMark = scrollBottomNow;
			start = eval(currentStart+iniSize);
			if(start>=total&&scrollObj.scrollHeight <= scrollObj.scrollTop + $(scrollObj).height()
					&&scrollTopNow!=scrollObj.scrollTop){
				if(!isBack)addPrompt(scrollObj,"Top&nbsp;<span style='font-size:18px;'>↑</span>","prompt",1);
				isBack = false;
				return;
			}
			scrollTopNow = scrollObj.scrollTop;
			if (scrollBottomNow<=scrollBottomMark/2) {
				if(loading==1||start>=total){
					return ;
				}
				loading = 1;
				var jsonstr = {'pager.start':start,'pager.length':incSize,'start':start,'length':incSize};
				for(var item in jsonstr){
					param[item] = jsonstr[item];
				}
				//addPrompt(scrollObj,"正在为您努力加载...","loading");
				table = tb;
				$.ajax({
					url:url,
					data:param,
					dataType:'text',
					type:'post',
					error:function(){},
					success:function(o){
						var r = eval('('+o+')');
						var list = r.data;
						if("callBack_"+formID in window){
							//jinjiayu20140523   隐藏“正在努力加载数据...”
							if($("#loadDiv").length>0){
								$("#loadDiv").hide();
							}
							if(eval("callBack_"+formID) instanceof Function)
							eval("callBack_"+formID+"("+JSON.stringify(r)+",2)");
							//重新注册表格滑动交替行
							//addTableClass();
							loading = 0;
							//$("#loading").remove();
							currentStart+=((total-start)<incSize?total-start+1:incSize);
							scrollBottomMark = ((scrollObj.scrollHeight-scrollObj.scrollTop-$(scrollObj).height())/scrollObj.scrollHeight)*$(scrollObj).height();
							topDoc$.scrollTop(customscrollcountoffsetTop);
							return ;
						}
						// 加载新的数据
						wd8.each(list, function(os, i) {
							//jinjiayu20140523   隐藏“正在努力加载数据...”
							if($("#loadDiv").length>0){
								$("#loadDiv").hide();
							}
							var row = table.insertRow(table.rows.length);
							if(i%2!=0)$(row).addClass("tdalt");
							wd8.each(os, function(o, j) {
								row.insertCell(j).innerHTML = o;
							});
						});
						//重新注册表格滑动交替行
						//addTableClass();
						loading = 0;
						//$("#loading").remove();
						currentStart+=((total-start)<incSize?total-start+1:incSize);
						scrollBottomMark = ((scrollObj.scrollHeight-scrollObj.scrollTop-$(scrollObj).height())/scrollObj.scrollHeight)*$(scrollObj).height();
						topDoc$.scrollTop(customscrollcountoffsetTop);
					}
				});
			}
		});
	}
	
	var getPointXY = function(obj){
		var scrollObj = obj;
		var point=new Object();
		point.x=scrollObj.offsetLeft;
		point.y=scrollObj.offsetTop+scrollObj.offsetHeight;
	 	while(scrollObj=scrollObj.offsetParent){
	  		point.x+=scrollObj.offsetLeft;
	  		point.y+=scrollObj.offsetTop;
	 	}
	 	return point;
	}
	
	var addPrompt = function(obj,content,id,back){
		if($("#"+id).is(":visible"))return ;
		var scrollObj = obj;
		var point=new Object();
		point.x=scrollObj.offsetLeft;
		point.y=scrollObj.offsetTop+scrollObj.offsetHeight;
	 	while(scrollObj=scrollObj.offsetParent){
	  		point.x+=scrollObj.offsetLeft;
	  		point.y+=scrollObj.offsetTop;
	 	}
	 	var x = ($(obj).width()-200)/2+point.x;
	 	var y = point.y-30;
	 	if(obj.scrollWidth>obj.clientWidth)y -= 20;
	 	if($("#"+id).length<=0){
		 	var RoundedCorner = "<div class='RoundedCorner ScrollPrompt' id='"+id+"'><b class='rtop'><b class='r1'></b><b class='r2'></b><b class='r3'></b><b class='r4'></b></b>"+content+"<b class='rbottom'><b class='r4'></b><b class='r3'></b><b class='r2'></b><b class='r1'></b></b></div>";
		 	document.body.insertAdjacentHTML("afterBegin",RoundedCorner);
	 	}
	 	$(".ScrollPrompt").css("cursor","pointer").css("top",y).css("left",x).css('display', 'block').fadeTo(0,0).fadeTo(1000,0.8).delay(2500).fadeOut(500,function(){isBack = false;});
	 	if(back==1)$("#"+id).click(function(){obj.scrollTop=0;$(this).remove();isBack = true;});
	}
	
	//初始化参数
	var initParam=function(p0,p1,p2,p3,p4,p5,p6,p7){
		scrollObj = p0;
		tb=p1;
		url=p2;
		if(p3!=undefined)param=p3;
		if(p4!=undefined)formID=p4;
		if(p5!=undefined)iniSize=parseInt(p5);
		if(p6!=undefined)incSize=parseInt(p6);
		if(p7!=undefined)ic=parseInt(p7);
	}
	return{
		'scroll':function(p0,p1,p2,p3,p4,p5,p6,p7){
					initParam(p0,p1,p2,p3,p4,p5,p6,p7);
					init();
					scroll();
				}
	}
})();